package org.jeecg.modules.chunk.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.chunk.entity.UploadFileParam;
import org.jeecg.modules.chunk.service.FileChunkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @ProjectName: jeecg-boot-parent
 * @ClassName: org.jeecg.modules.chunk.controller.FileChunkController
 * @Version: 1.0
 */
@RestController
@RequestMapping("/fileChunk")
@Api(tags = "bim文件上传")
public class FileChunkController {

    @Autowired
    private FileChunkService fileChunkService;

    /**
     * 模型文件上传获取分片
     *
     * @param requestParam
     */
    @AutoLog(value = "模型文件上传获取分片")
    @ApiOperation(value = "模型文件上传获取分片", notes = "模型文件上传获取分片")
    @PostMapping(value = "/upload")
    public String upload(@RequestBody UploadFileParam requestParam) {
        return fileChunkService.uploadFile(requestParam);
    }

    /**
     * 分片文件上传
     *
     * @param request
     */
    @AutoLog(value = "文件上传")
    @ApiOperation(value = "文件上传", notes = "文件上传")
    @PutMapping(value = "/put")
    public Result<?> upload(HttpServletRequest request) {
        String chunkUrl = request.getParameter("chunkUrl");
        Map<String, String[]> parameterMap = request.getParameterMap();
        CommonsMultipartResolver resolver = new CommonsMultipartResolver(
                request.getSession().getServletContext());
        MultipartFile file = null;
        if (resolver.isMultipart(request)) {
            MultipartHttpServletRequest mult = (MultipartHttpServletRequest) request;
            Iterator it = mult.getFileNames();
            while (it.hasNext()) {
                file = mult.getFile(it.next().toString());
            }
        }
        return Result.OK();
    }

}